package com.sjy.ylxt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sjy.ylxt.entity.basic.ResultBody;
import com.sjy.ylxt.entity.dto.AddAdminDTO;
import com.sjy.ylxt.entity.dto.QueryAdminDTO;
import com.sjy.ylxt.enums.DeleteFlagEnum;
import com.sjy.ylxt.factory.O2O;
import com.sjy.ylxt.utils.MD5Util;
import com.sjy.ylxt.utils.SnowFlakeUtil;
import com.sjy.ylxt.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import com.sjy.ylxt.entity.domain.YlxtAdmin;
import com.sjy.ylxt.mapper.YlxtAdminMapper;
import com.sjy.ylxt.service.YlxtAdminService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author: 史俊颖
 * @CreateTime: 2024-12-12
 * @Description: ${description}
 */
@Service
@Slf4j
public class YlxtAdminServiceImpl extends ServiceImpl<YlxtAdminMapper, YlxtAdmin> implements YlxtAdminService {

    @Resource
    private YlxtAdminMapper ylxtAdminMapper;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return ylxtAdminMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(YlxtAdmin record) {
        return ylxtAdminMapper.insert(record);
    }

    @Override
    public int insertSelective(YlxtAdmin record) {
        return ylxtAdminMapper.insertSelective(record);
    }

    @Override
    public YlxtAdmin selectByPrimaryKey(Long id) {
        return ylxtAdminMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(YlxtAdmin record) {
        return ylxtAdminMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(YlxtAdmin record) {
        return ylxtAdminMapper.updateByPrimaryKey(record);
    }

    /**
     * 添加管理员
     *
     * @param addAdminDTO
     * @return
     */
    @Override
    public ResultBody<?> addAdmin(AddAdminDTO addAdminDTO) {
        try {
            log.info("==> 开始添加管理员");
            addAdminDTO.setPassWord(addAdminDTO.getPassWord());
            YlxtAdmin ylxtAdmin = O2O.AdminDtoToEntity(addAdminDTO);
            //todo 待补充 获取创建用户信息
            ylxtAdmin.setDeleteFlag(DeleteFlagEnum.UN_DELETE_FLAG.getCode());
            ylxtAdmin.setCreateUser("admin");
            ylxtAdmin.setUpdateUser("admin");
            ylxtAdmin.setCreateTime(new Date());
            ylxtAdmin.setUpdateTime(new Date());
            ylxtAdminMapper.insert(ylxtAdmin);
            return ResultBody.success();
        } catch (Exception e) {
            log.info("==> 添加管理员失败，失败原因为:", e);
            throw new RuntimeException(e);
        }
    }

    /**
     * @return
     */
    @Override
    public ResultBody<?> queryAdmin(Integer pageNum,Integer pageSize) {
        try {
            log.info("==> 开始分页查询管理员列表");
//            PageHelper.startPage(queryAdminDto.getPageNum(), queryAdminDto.getPageSize());
            PageHelper.startPage(pageNum, pageSize);
            LambdaQueryWrapper<YlxtAdmin> qw = new LambdaQueryWrapper<>();
//            qw.eq(StringUtils.isNotEmpty(queryAdminDto.getPhoneNumber()), YlxtAdmin::getPhoneNumber, queryAdminDto.getPhoneNumber());
//            qw.eq(StringUtils.isNotEmpty(queryAdminDto.getAdminName()), YlxtAdmin::getAdminName, queryAdminDto.getAdminName());
            qw.eq(YlxtAdmin::getDeleteFlag, DeleteFlagEnum.UN_DELETE_FLAG.getCode());
            qw.orderByDesc(YlxtAdmin::getCreateTime);
            List<YlxtAdmin> ylxtAdmins = ylxtAdminMapper.selectList(qw);
            PageInfo<YlxtAdmin> pageInfo = new PageInfo<>(ylxtAdmins);
            return ResultBody.success(pageInfo);
        } catch (Exception e) {
            log.error("==> 查询管理员列表报错, 报错信息为: {}", e.getMessage(), e);
            return ResultBody.error("查询管理员列表失败");
        }
    }

    /**
     * @param id
     * @return
     */
    @Override
    public ResultBody<?> disable(String id) {
        try{
            log.info("==> 开始禁用管理员");
            YlxtAdmin ylxtAdmin = ylxtAdminMapper.selectByPrimaryKey(Long.valueOf(id));
            ylxtAdmin.setDeleteFlag(DeleteFlagEnum.DELETE_FLAG.getCode());
            ylxtAdminMapper.updateByPrimaryKey(ylxtAdmin);
            log.info("==> 禁用管理员成功");
            return ResultBody.success();
        }catch (Exception e){
            log.error("==> 禁用管理员失败,失败信息为：{}",e.getMessage());
            return ResultBody.error("禁用管理员失败");
        }
    }

    /**
     * @param id
     * @return
     */
    @Override
    public ResultBody<?> enable(String id) {
        try{
            log.info("==> 开始启用管理员");
            YlxtAdmin ylxtAdmin = ylxtAdminMapper.selectByPrimaryKey(Long.valueOf(id));
            ylxtAdmin.setDeleteFlag(DeleteFlagEnum.UN_DELETE_FLAG.getCode());
            ylxtAdminMapper.updateByPrimaryKey(ylxtAdmin);
            log.info("==> 启用管理员成功");
            return ResultBody.success();
        }catch (Exception e){
            log.error("==> 启用管理员失败,失败信息为：{}",e.getMessage());
            return ResultBody.error("启用管理员失败");
        }
    }

    /**
     * @param id
     * @return
     */
    @Override
    public ResultBody<?> detail(String id) {
        try {
            log.info("==> 开始查询管理员详情");
            YlxtAdmin ylxtAdmin = ylxtAdminMapper.selectByPrimaryKey(Long.valueOf(id));
            return ResultBody.success(ylxtAdmin);
        } catch (Exception e) {
            log.error("==> 查询管理员详情失败,失败信息为：{}", e.getMessage());
            return ResultBody.error("查询管理员详情失败");
        }
    }
}
